<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2021/1/28
 * Time: 16:34
 * 特惠商品用户
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Memberh extends Base
{
    /**
     * 特惠商品用户列表
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $keyword = trim(input('keyword'), ' '); // 用户ID/手机号/姓名/昵称
            $user_gender = input('user_gender'); // 性别   1 男   2 女   3 未知
            $user_sheng = input('user_sheng');
            $user_shi = input('user_shi');
            $user_qu = input('user_qu');
            $start_time = input('start_time'); // 注册开始时间
            $end_time = input('end_time'); // 注册结束时间
            $login_start_time = input('login_start_time'); // 登录开始时间
            $login_end_time = input('login_end_time'); // 登录结束时间
            
            $where['user_rule'] = [['=', 2], ['=', '2,3'], 'or'];
            $where['user_status'] = ['NEQ', 3];
            $where['user_private_worker'] = 1;
            // 关键字
            if(isset($keyword) && $keyword != ''){
                $where['user_id|user_phone|user_username|user_real_name'] = ['like', '%' . $keyword . '%'];
            }
            // 性别
            if(isset($user_gender) && $user_gender != ''){
                $where['user_gender'] = $user_gender;
            }
            // 省
            if(isset($user_sheng) && $user_sheng != ''){
                $where['user_sheng'] = $user_sheng;
            }
            // 市
            if(isset($user_shi) && $user_shi != ''){
                $where['user_shi'] = $user_shi;
            }
            // 区
            if(isset($user_qu) && $user_qu != ''){
                $where['user_qu'] = $user_qu;
            }
            // 注册时间
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time != ''){
                $where['user_time'] = ['between', [$start_time, $end_time]];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time == ''){
                $where['user_time'] = ['>=', $start_time];
            }
            if(isset($start_time) && $start_time == '' && isset($end_time) && $end_time != ''){
                $where['user_time'] = ['<=', $end_time];
            }
            // 登录时间
            if(isset($login_start_time) && $login_start_time != '' && isset($login_end_time) && $login_end_time != ''){
                $where['user_last_login'] = ['between', [$login_start_time, $login_end_time]];
            }
            if(isset($login_start_time) && $login_start_time != '' && isset($login_end_time) && $login_end_time == ''){
                $where['user_last_login'] = ['>=', $login_start_time];
            }
            if(isset($login_start_time) && $login_start_time == '' && isset($login_end_time) && $login_end_time != ''){
                $where['user_last_login'] = ['<=', $login_end_time];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_u_user')
                ->where($where)
                ->count('user_id');// 获取总条数
            $lists = Db::table('up_u_user')
                ->field('user_id, user_username, user_phone, user_area_str, user_gender, 
                user_time, user_real_name, user_status, user_last_login')
                ->where($where)
                ->order('user_time desc')
                ->page($Nowpage,$limits)
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $province = Db::table('up_area')->where('level', 1)->field('district_id, district')->select();
        $this->assign([
            'province' => $province
        ]);
        return $this->fetch();
    }
    
    /**
     * 详情
     * @return mixed
     */
    public function userDetail(){
        $user_id = input('user_id');
        $user = Db::table('up_u_user')->where('user_id', $user_id)
            ->field('user_username, user_image, user_phone, user_gender, user_real_name, user_shop_name, 
            user_time, user_area_str, user_address, user_status, user_total_balance, user_recharge_balance')
            ->find();
        
        return $this->fetch('memberh/userdetail', ['user' => $user]);
    }
    
    /**
     * 编辑用户
     * @return mixed|\think\response\Json
     */
    public function userEdit(){
        $user_id = input('user_id');
        $user = Db::table('up_u_user')->where('user_id', $user_id)
            ->field('user_username, user_image, user_phone, user_gender, user_real_name, user_shop_name, 
            user_address, user_sheng, user_shi, user_qu, user_bank_username, 
            user_bank_name, user_bankcard_code')
            ->find();
        if(Request::instance()->isPost()){
            $user_image = input('user_image');
            $user_phone = input('user_phone');
            $user_password = input('user_password');
            $user_username = input('user_username');
            $user_gender = input('user_gender');
            $user_shop_name = input('user_shop_name');
            $user_sheng = input('user_sheng');
            $user_shi = input('user_shi');
            $user_qu = input('user_qu');
            $user_address = input('user_address');
            $user_bank_username = input('user_bank_username');
            $user_bank_name = input('user_bank_name');
            $user_bankcard_code = input('user_bankcard_code');
            $sheng_name = Db::table('up_area')->where('district_id', $user_sheng)->value('district');
            $shi_name = Db::table('up_area')->where('district_id', $user_shi)->value('district');
            $qu_name = Db::table('up_area')->where('district_id', $user_qu)->value('district');
            $address = Db::table('up_user_address')
                ->where(['addr_user_id' => $user_id, 'addr_from' => 1, 'addr_del' => 1])
                ->field('addr_id')
                ->find();
            
            $exist = Db::table('up_u_user')
                ->where(['user_phone' => $user_phone, 'user_status' => 1, 'user_id' => ['NEQ', $user_id]])
                ->find();
            if($exist) return json(['code' => 100, 'msg' => '已有相同手机号']);
            
            $update = [
                'user_phone' => $user_phone,
                'user_username' => $user_username,
                'user_gender' => $user_gender,
                'user_shop_name' => $user_shop_name,
                'user_sheng' => $user_sheng,
                'user_shi' => $user_shi,
                'user_qu' => $user_qu,
                'user_area_str' => $sheng_name . $shi_name . $qu_name,
                'user_address' => $user_address,
                'user_bank_username' => $user_bank_username,
                'user_bank_name' => $user_bank_name,
                'user_bankcard_code' => $user_bankcard_code,
                'user_update_time' => date('Y-m-d H:i:s')
            ];
            if(!empty($user_image)) $update['user_image'] = $user_image;
            if(!empty($user_password)){
                $update['user_password'] = md5($user_password);
                $update['user_password_see'] = $user_password;
            }
            
            Db::startTrans();
            try{
                // 修改用户信息
                Db::table('up_u_user')->where('user_id', $user_id)->update($update);
                
                // 修改收货地址信息
                if($address){
                    $update_addr = [
                        'addr_user_phone' => $user_phone,
                        'addr_sheng' => $user_sheng,
                        'addr_shi' => $user_shi,
                        'addr_qu' => $user_qu,
                        'addr_address' => $user_address,
                        'addr_updatetime' => date('Y-m-d H:i:s')
                    ];
                    Db::table('up_user_address')->where('addr_id', $address['addr_id'])->update($update_addr);
                }
                Db::commit();
                return json(['code' => 200, 'msg' => '编辑成功']);
            }catch(\Exception $ee){
                Db::rollback();
                return json(['code' => 100, 'msg' => $ee->getMessage()]);
            }
        }
        
        $province = Db::table('up_area')->where('level', 1)->field('district_id, district')->select();
        $city = Db::table('up_area')->where('pid', $user['user_sheng'])->field('district_id, district')->select();
        $qu = Db::table('up_area')->where('pid', $user['user_shi'])->field('district_id, district')->select();
        $this->assign([
            'user' => $user,
            'user_id' => $user_id,
            'province' => $province,
            'city' => $city,
            'qu' => $qu,
        ]);
        return $this->fetch('memberh/useredit');
    }
    
    /**
     * 冻结
     * @return \think\response\Json
     */
    public function userFreeze(){
        $user_id = input('id');
        $res = Db::table('up_u_user')->where('user_id', $user_id)->setField('user_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '冻结成功']);
        }else{
            return json(['code' => 100, 'msg' => '冻结失败']);
        }
    }
    
    /**
     * 启用
     * @return \think\response\Json
     */
    public function userUsing(){
        $user_id = input('id');
        $res = Db::table('up_u_user')->where('user_id', $user_id)->setField('user_status', 1);
        if($res){
            return json(['code' => 200, 'msg' => '启用成功']);
        }else{
            return json(['code' => 100, 'msg' => '启用失败']);
        }
    }
    
    /**
     * 收货地址---作废
     * @return mixed|\think\response\Json
     */
    public function userAddress(){
        if(request()->isAjax ()){
            $where['addr_user_id'] = session('h_user_id');
            $where['addr_del'] = 1;
            $where['addr_from'] = 2;
            
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_user_address')
                ->join('up_area u1', 'u1.district_id = addr_sheng', 'LEFT')
                ->join('up_area u2', 'u2.district_id = addr_shi', 'LEFT')
                ->join('up_area u3', 'u3.district_id = addr_qu', 'LEFT')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::table('up_user_address')
                ->join('up_area u1', 'u1.district_id = addr_sheng', 'LEFT')
                ->join('up_area u2', 'u2.district_id = addr_shi', 'LEFT')
                ->join('up_area u3', 'u3.district_id = addr_qu', 'LEFT')
                ->field('addr_id, addr_user_name, addr_user_phone, u1.district sheng, u2.district shi, 
                u3.district qu, addr_address, addr_is_default, addr_time')
                ->where($where)
                ->order('addr_is_default asc, addr_time desc')
                ->page($Nowpage,$limits)
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $user_id = input('user_id');
        if(isset($user_id) && $user_id != '') session('h_user_id', $user_id);
        return $this->fetch('memberh/useraddress');
    }
    
    /**
     * 下级列表---作废
     * @return mixed|\think\response\Json
     */
    public function userLower(){
        if(Request::instance()->isAjax()){
            $user_invite = Db::table('up_u_user')->where('user_id', session('h_user_id'))->value('user_invite');
            if($user_invite){
                $where['user_invite_other'] = $user_invite;
                $Nowpage = input('page') ? input('page') : 1;
                $limits = input("limit") ? input("limit") : 10;
                $count = Db::table('up_u_user')
                    ->where($where)
                    ->count();// 获取总条数
                $lists = Db::table('up_u_user')
                    ->field('user_id, user_real_name, user_phone')
                    ->where($where)
                    ->page($Nowpage,$limits)
                    ->select();
            }else{
                $Nowpage = input('page') ? input('page') : 1;
                $limits = input("limit") ? input("limit") : 10;
                $count = 0;
                $lists = [];
            }
            
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $user_id = input('user_id');
        if(isset($user_id) && $user_id != '') session('h_user_id', $user_id);
        return $this->fetch('memberh/userlower');
    }
    
    /**
     * 选择下级---作废
     * @return mixed|\think\response\Json
     */
    public function lowerAdd(){
        $user_invite = Db::table('up_u_user')->where('user_id', session('h_user_id'))->value('user_invite');
        if(Request::instance()->isPost()){
            $user_id = input('user_id');
            
            $update = [
                'user_invite_other' => $user_invite
            ];
            $res = Db::table('up_u_user')->where('user_id', $user_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '选择成功']);
            }else{
                return json(['code' => 100, 'msg' => '选择失败']);
            }
        }
        $user_invite_other = Db::table('up_u_user')->where('user_id', session('h_user_id'))->value('user_invite_other');
        $where = [
            'user_status' => 1,
            'user_id' => ['NEQ', session('h_user_id')],
            'user_invite_other' => ['EQ', '0'],
        ];
        if($user_invite_other){
            $where['user_invite'] = ['NEQ', $user_invite_other];
        }
        $user = Db::table('up_u_user')
            ->where($where)
            ->field('user_id, user_real_name, user_phone')
            ->select();
        return $this->fetch('memberh/loweradd', ['user' => $user]);
    }
    
    /**
     * 删除下级---作废
     * @return \think\response\Json
     */
    public function lowerDel(){
        $user_id = input('id');
        $res = Db::table('up_u_user')->where('user_id', $user_id)->setField('user_invite_other', '0');
        if($res){
            return json(['code' => 200, 'msg' => '移除成功']);
        }else{
            return json(['code' => 100, 'msg' => '移除失败']);
        }
    }
    
    /**
     * 修改佣金---作废
     * @return mixed|\think\response\Json
     */
    public function userYj(){
        $user_id = input('user_id');
        if(Request::instance()->isPost()){
            $yj = input('yj');
            $res = Db::table('up_u_user')->where('user_id', $user_id)->setField('user_fy_balance', $yj);
            if($res){
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        $user = Db::table('up_u_user')->where('user_id', $user_id)->field('user_fy_balance')->find();
        return $this->fetch('memberh/useryj', ['user_id' => $user_id, 'user' => $user]);
    }
    
    /**
     * 导出
     * @return \think\response\Json
     */
    public function outExcel(){
        set_time_limit(0);
        $user_ids = input('ids');

        $where['user_rule'] = [['=', 2], ['=', '2,3'], 'or'];
        $where['user_status'] = ['NEQ', 3];
        $where['user_private_worker'] = 1;

        if($user_ids == -1){
            $keyword = trim(input('keyword'), ' '); // 用户ID/手机号/姓名/昵称
            $user_gender = input('user_gender'); // 性别   1 男   2 女   3 未知
            $user_sheng = input('user_sheng');
            $user_shi = input('user_shi');
            $user_qu = input('user_qu');
            $start_time = input('start_time'); // 注册开始时间
            $end_time = input('end_time'); // 注册结束时间
            $login_start_time = input('login_start_time'); // 登录开始时间
            $login_end_time = input('login_end_time'); // 登录结束时间

            // 关键字
            if(isset($keyword) && $keyword != ''){
                $where['user_id|user_phone|user_username|user_real_name'] = ['like', '%' . $keyword . '%'];
            }
            // 性别
            if(isset($user_gender) && $user_gender != ''){
                $where['user_gender'] = $user_gender;
            }
            // 省
            if(isset($user_sheng) && $user_sheng != ''){
                $where['user_sheng'] = $user_sheng;
            }
            // 市
            if(isset($user_shi) && $user_shi != ''){
                $where['user_shi'] = $user_shi;
            }
            // 区
            if(isset($user_qu) && $user_qu != ''){
                $where['user_qu'] = $user_qu;
            }
            // 注册时间
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time != ''){
                $where['user_time'] = ['between', [$start_time, $end_time]];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time == ''){
                $where['user_time'] = ['>=', $start_time];
            }
            if(isset($start_time) && $start_time == '' && isset($end_time) && $end_time != ''){
                $where['user_time'] = ['<=', $end_time];
            }
            // 登录时间
            if(isset($login_start_time) && $login_start_time != '' && isset($login_end_time) && $login_end_time != ''){
                $where['user_last_login'] = ['between', [$login_start_time, $login_end_time]];
            }
            if(isset($login_start_time) && $login_start_time != '' && isset($login_end_time) && $login_end_time == ''){
                $where['user_last_login'] = ['>=', $login_start_time];
            }
            if(isset($login_start_time) && $login_start_time == '' && isset($login_end_time) && $login_end_time != ''){
                $where['user_last_login'] = ['<=', $login_end_time];
            }
        }else{
            if($user_ids == '') return json(['code' => 100, 'msg' => '请选择需要导出的数据']);
            $where['user_id'] = ['IN', $user_ids];
        }
    
        $list = Db::table('up_u_user')
            ->field('user_id, user_username, user_phone, user_area_str, user_gender, 
                user_time, user_real_name, user_status, user_last_login, user_shop_name, user_address, 
                user_rule, user_recharge_balance, user_invite, user_bank_username, user_bank_name, 
                user_bankcard_code, user_total_balance')
            ->where($where)
            ->select();
        
        foreach($list as $k => $v){
            // 手机号
            $list[$k]['user_phone'] = "\t" . $v['user_phone'] . "\t";
            // 性别
            switch($v['user_gender']){
                case 1:
                    $list[$k]['user_gender'] = '男';
                    break;
                case 2:
                    $list[$k]['user_gender'] = '女';
                    break;
                case 3:
                    $list[$k]['user_gender'] = '未知';
                    break;
            }
            // 状态
            $list[$k]['user_status'] = $v['user_status'] == 1 ? '正常' : '冻结';
            // 银行卡
            $list[$k]['user_bankcard_code'] = "\t" . $v['user_bankcard_code'] . "\t";
        }
    
        $cellname = [
            ['user_username', '昵称', 15, 'LEFT'],
            ['user_phone', '手机号', 18, 'LEFT'],
            ['user_real_name', '姓名', 15, 'LEFT'],
            ['user_gender', '性别', 10, 'LEFT'],
            ['user_status', '状态', 10, 'LEFT'],
            ['user_area_str', '地区', 40, 'LEFT'],
            ['user_address', '详细地址', 30, 'LEFT'],
            ['user_time', '注册时间', 18, 'LEFT'],
            ['user_last_login', '最后登录时间', 18, 'LEFT'],
            ['user_invite', '邀请码', 15, 'LEFT'],
            ['user_shop_name', '店铺名称', 20, 'LEFT'],
            ['user_total_balance', '总积分', 15, 'LEFT'],
            ['user_recharge_balance', '可用积分', 15, 'LEFT'],
            ['user_bank_name', '开户行', 12, 'LEFT'],
            ['user_bank_username', '开户名', 12, 'LEFT'],
            ['user_bankcard_code', '卡号', 22, 'LEFT']
        ];
        
        $res = exportExcelZhy('用户表', 'Member', $cellname, $list);
        return json($res);
    }
}